package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserImpl implements UserDao {
    @Override
    public int add(UserModel userModel) {
        String sql = "insert into user (username,password) values('" + userModel.getUsername() + "','" + userModel.getPassword() + "')";
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public UserModel findByName(String username) {
        String sql = "select * from user where username='" + username + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel userModel = null;
        try {
            while (resultSet.next()) {
                userModel = new UserModel();//这句话就已经给userModel进行的赋值，新建了一个对象
//                userModel.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userModel;
    }

    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
        String sql = "select * from user where username='" + userModel.getUsername() + "' and password='" + userModel.getPassword() + "'";
        ResultSet resultSet = JDBC.getResultSet(sql);
        UserModel model = null;
        try {
            while (resultSet.next()) {
                model = new UserModel();//这句话就已经给userModel进行的赋值，新建了一个对象
//                userModel.setUsername(resultSet.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }
}
